<?php
class DB
{
	var $connection = '';
	var $querycount = 0;
	var $totaltime = 0;
	var $errorcode = 0;
	var $errormsg = '';
	var $resultset = '';
	
	function DB($host, $user, $pass, $name)
	{
		$starttime = $this -> getMicroTime();
		
		if(!$this -> connection = @mysql_connect($host, $user, $pass, true))
		{
			$this -> errorcode = mysql_errno();
			$this -> errormsg = mysql_error();
			
			return false;
		}else{
			mysql_query('SET NAMES UTF8');
		}
		
		if(!@mysql_select_db($name, $this -> connection))
		{
			$this -> errorcode = mysql_errno();
			$this -> errormsg = mysql_error();
			@mysql_close($this -> connection);
			
			return false;
		}
		
		$this -> totaltime += $this -> getMicroTime() - $starttime;
		
		return true;
	}
	
	function db_query($sql)
	{
		$starttime = $this -> getMicroTime();
		++$this -> querycount;
		
		if(!$this -> resultset = @mysql_query($sql, $this -> connection))
		{
			$this -> errorcode = mysql_errno();
			$this -> errormsg = mysql_error();
			$this -> totaltime = $this -> getMicroTime() - $starttime;
			
			return false;
		}
		
		$this -> totaltime += $this -> getMicroTime() - $starttime;
		
		return $this -> resultset;
	}
	
	function db_aff_rows()
	{
		return @mysql_affected_rows($this -> connection);
	}
	
	function db_rows()
	{
		return @mysql_num_rows($this -> resultset);
	}
	
	function db_ins_id()
	{
		return @mysql_insert_id($this -> connection);
	}
	
	function db_result()
	{
		$array = array();
		
		while($row = mysql_fetch_assoc($this -> resultset))
		{
				$array[] = $row;
		}
		
		mysql_free_result($this -> resultset);
		
		return $array;
	}
	
	function db_error_code()
	{
		return $this -> errorcode;
	}
	
	function db_error_msg()
	{
		return $this -> errormsg;
	}
	
	function db_time()
	{
		return round($this -> totaltime, 6);
	}
	
	function db_count()
	{
		return $this -> querycount;
	}
	
	function getMicroTime()
	{
		return time();
	}
	function closedb(){
		mysql_free_result($this -> connection);
		return mysql_close($this -> connection);
	}
	
	function check_table($tbl)
	{
		$tables = array();
				
		$q = @mysql_query("SHOW TABLES");
		
		while ($r = @mysql_fetch_array($q)) 
		{ 
			$tables[] = $r[0]; 
		}
		
		if (in_array($tbl, $tables)) 
		{ 
			return TRUE; 
		}
		else 
		{ 
			return FALSE; 
		}
	}
}
?>